/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.component;

import java.util.Date;

import com.cleangwt.bootstrap.client.ui.Jumbotron;
import com.cleangwt.bootstrap.client.ui.Navbar;
import com.cleangwt.bootstrap.template.client.NavbarAffixedContainer1;
import com.cleangwt.bootstrap.template.client.NavbarAffixedContainer2;
import com.cleangwt.bootstrap.template.client.NavbarAffixedContainer3;
import com.cleangwt.bootstrap.template.client.NavbarSidebarContainer1;
import com.cleangwt.page.client.PageContainer;
import com.cleangwt.page.client.PageContainerFilter;
import com.cleangwt.showcase.client.ProvidesSource;
import com.cleangwt.showcase.client.resource.DataSource;
import com.cleangwt.user.client.ui.ListBox;
import com.cleangwt.user.shared.Option;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class ShowcasePageFilter extends PageContainerFilter implements ProvidesSource {

  private Widget createLayoutControl() {
    ListBox list = new ListBox();
    list.addItem("PageContainer1", "NavbarFixed1");
    list.addItem("PageContainer2", "NavbarFixed2");
    list.addItem("PageContainer3", "NavbarFixed3");
    list.addOption(new Option("FullPageContainer", "NavbarStatic1", true));
    if (Cookies.getCookie("layout") != null) {
      list.setValue(Cookies.getCookie("layout"));
    }
    list.addValueChangeHandler(new ValueChangeHandler<String>() {

      @Override
      public void onValueChange(ValueChangeEvent<String> event) {
        String layout = event.getValue();
        if (layout.equals(Cookies.getCookie("layout"))) {
          return;
        }
        Cookies.setCookie("layout", layout);
        // refresh
        History.fireCurrentHistoryState();
      }
    });
    return list;
  }

  private Widget createLocaleControl() {
    final ListBox list = new ListBox();
    // default
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    if (currentLocale.equals("default")) {
      currentLocale = "en";
    }
    String[] localeNames = LocaleInfo.getAvailableLocaleNames();
    for (String localeName : localeNames) {
      if (!localeName.equals("default")) {
        String nativeName = LocaleInfo.getLocaleNativeDisplayName(localeName);
        list.addOption(new Option(nativeName, localeName));
        if (localeName.equals(currentLocale)) {
          list.setSelectedIndex(list.getItemCount() - 1);
        }
      }

    }
    list.addValueChangeHandler(new ValueChangeHandler<String>() {

      @SuppressWarnings("deprecation")
      @Override
      public void onValueChange(ValueChangeEvent<String> event) {
        String localeName = list.getValue();
        if (LocaleInfo.getLocaleCookieName() != null) {
          // expire in one year
          Date expires = new Date();
          expires.setYear(expires.getYear() + 1);
          Cookies.setCookie(LocaleInfo.getLocaleCookieName(), localeName, expires);
        }

        UrlBuilder builder =
            Location.createUrlBuilder().setParameter(LocaleInfo.getLocaleQueryParam(), localeName);
        Window.Location.replace(builder.buildString());
      }
    });
    return list;
  }

  @Override
  protected String getContainerName(String pageName) {
    if ("Welcome".equals(pageName)) {
      return null;
    }
    if ("PageContextTutorial".equals(pageName)) {
      Cookies.setCookie("layout", "NavbarStatic1");
    } else {
      String layout = Cookies.getCookie("layout");
      if (layout == null || "NavbarStatic1".equals(layout)) {
        Cookies.setCookie("layout", "NavbarFixed1");
      }
    }
    return Cookies.getCookie("layout");
  }

  protected PageContainer getPageContainer(String name) {
    if ("NavbarFixed1".equals(name)) {
      NavbarAffixedContainer1 container =
          new NavbarAffixedContainer1(Navbar.DEFAULT_STYLE, DataSource.getLayeredLinks3());
      container.setHeading(new Jumbotron("PageContainer1",
          "The page conatiner with navbar and affixed sidebar for 3 layered links."));
      initNavbar(container.getNavbar());
      return container;
    } else if ("NavbarFixed2".equals(name)) {
      NavbarAffixedContainer2 container =
          new NavbarAffixedContainer2(Navbar.INVERSE_STYLE, DataSource.getLayeredLinks2());
      container.setHeading(new Jumbotron("PageContainer2",
          "The page conatiner with navbar and affixed sidebar for 2 layered links."));
      initNavbar(container.getNavbar());
      return container;
    } else if ("NavbarFixed3".equals(name)) {
      NavbarAffixedContainer3 container =
          new NavbarAffixedContainer3(Navbar.DEFAULT_STYLE, DataSource.getLayeredLinks2());
      initNavbar(container.getNavbar());
      return container;
    } else if ("NavbarStatic1".equals(name)) {
      NavbarSidebarContainer1 container =
          new NavbarSidebarContainer1(Navbar.DEFAULT_STYLE, DataSource.getLayeredLinks3());
      initNavbar(container.getNavbar());
      return container;
    }
    throw new IllegalArgumentException(name);
  }

  private void initNavbar(Navbar navbar) {
    navbar.setBrand("CleanGWT");
    navbar.addForm(true).addFormControl(this.createLayoutControl());
    navbar.addForm(true).addFormControl(this.createLocaleControl());
  }

}
